package cn.learn.validate.image;

import cn.learn.properties.ImageCodeProperties;
import cn.learn.properties.SecurityProperties;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 配置图形化验证码kaptcha的配置类
 *
 * @author huangyezhan
 * @date 2020年02月20日1:22
 */
@Configuration
public class KaptchaConfig {

    private ImageCodeProperties image;

    @Autowired
    public void setSecurityProperties(SecurityProperties securityProperties) {
        this.image = securityProperties.getCode().getImage();
    }


    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Config         config         = new Config(getKaptchaPropertes());
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    /**
     * 获取Kaptcha的默认属性(配置默认参数)
     *
     * @return 返回配置属性的对象
     */
    public Properties getKaptchaPropertes() {
        Properties properties = new Properties();
        //边框属性：边框(yes、no)、颜色、厚度(>0)
        properties.setProperty("kaptcha.border", "no");
        properties.setProperty("kaptcha.border.color", "105,179,90");
        properties.setProperty("kaptcha.border.thickness", "1");
        //图片的宽、高
        properties.setProperty("kaptcha.image.width", String.valueOf(image.getLength()));
        properties.setProperty("kaptcha.image.height", String.valueOf(image.getWith()));

        //字体：颜色、大小、间隔、字体类型
        properties.setProperty("kaptcha.textproducer.font.color", "red");
        properties.setProperty("kaptcha.textproducer.font.size", "40");
        properties.setProperty("kaptcha.textproducer.char.space", "8");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        //几个字符
        properties.setProperty("kaptcha.textproducer.char.length", String.valueOf(image.getCount()));

        //properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");

        properties.setProperty("kaptcha.session.key", "code");
        return properties;
    }

}